<?php

namespace App\Http\Controllers\Mobile;

use Illuminate\Support\Facades\Log;
use App\Models\Order;
use App\Models\OrderProduct;
use App\Models\UserAddress;
use App\Repos\OrderRepo;
use App\Repos\UserRepo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
    	$wechatUser  = session('wechat.oauth_user.default');
        $user        = UserRepo::findByOpenid($wechatUser->id);
        $where['user_id']   = $user->id;


        $rs= OrderRepo::getOrderList($where);


        return view('mobile.order',compact('rs'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $cartParams = $request->except('_token');
        $wechatUser  = session('wechat.oauth_user.default');
        $user        = UserRepo::findByOpenid($wechatUser->id);

        $where['user_id']   = $user->id;
        $userAddress = UserAddress::where($where)->orderBy('is_default','desc')->first();


        return view('mobile.confirm',compact('cartParams','userAddress'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $rs['status'] = 'danger';
        $rs['data']   = [];
        //暂为单个商品

        $wechatUser  = session('wechat.oauth_user.default');
        $user        = UserRepo::findByOpenid($wechatUser->id);

        $data['user_id'] = $user->id;

        //数量
        $data['total_amount'] =  $request->buyNum;
        //总价
        $data['total_price']  = $request->allPrice;

        //订单号
        $data['code']     = date('YmdHis');
        //收货地址
        $data['truename'] = $request->truename;
        $data['mobile']   = $request->mobile;
        $data['address']  = $request->address;

        $data['status']   = 1;

        //订单入库
        $order = Order::create($data);

        //商品出库
        $op['order_id']    = $order->id;
        $op['product_id']  = $request->productId;
        $op['product_num'] = $request->buyNum;
        $op['product_price'] = $request->price;
        $op['product_name']  = $request->productName;
        $op['product_thumb'] = $request->thumb;
        //$op['spec_values'] = $request->specName;
        $op['subtotal']    = $request->allPrice;

        $orderProduct = OrderProduct::create($op);

        Log::info('订单金额:'.$data['total_price']);
        if($order && $orderProduct){
            $rs['status'] = 'success';
            $rs['data']   = $data;
        }

        return response()->json($rs);


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
